<?php
abstract class CommonController extends AbstractController
{
    protected $uid = 0;
    protected $isAjax = false;
	public function _init()
	{
	    $this->isAjax = Functions::gpc('isAjax', 'g', 0) == 0 ? false : true;
	    $this->view->setIsAjax($this->isAjax);
		$this->_sendNoCache();
	    $this->_checkLogin();
		$this->_initCommonData();
		$this->view->setPlace('left', 'place/left_account');
	}
	
	private function _initCommonData()
	{
		$accountCls = new AccountModel($this->uid);
		$account = $accountCls->getAccounts();
		
		$recordCls = new RecordModel($this->uid);
		$record = $recordCls->getAll();
		
		$userCls = new PassportModel();
		$userInfo = $userCls->getUserInfoByUid($this->uid);
		
		$this->view->assign('title', '您身边的理财助手');
		$this->view->assign('LeftAccount', $account);
		$this->view->assign('RecordData', $record);
		
		$this->view->assign('UserInfo', $userInfo);
	}
	private function _checkLogin()
	{
	    $passportModelCls = new PassportModel();
	    $uid = $passportModelCls->isLogined();
	    if(!$uid)
	    {
            $this->_noLogin();
	    }
	    $this->uid = $uid;
	}
	
	protected function _noLogin()
	{
		$this->view->setAutoDisplay(false);
		$this->_redirect(Config::getConfig('WEB_DOMAIN_ROOT').'/passport.php');
	}
	
	protected function _sendNoCache()
	{
		header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
		header("Cache-Control: no-cache");
		header("Pragma: no-cache");
	}
	
	protected function _askConfirm($confirmUrl, $cancelUrl, $enquiry = '确认执行该操作？', $template = 'common/askConfirm.php')
	{
		$this->view->setAutoDisplay(false);
		$this->view->assign('ConfirmUrl', $confirmUrl);
		$this->view->assign('CancelUrl', $cancelUrl);
		$this->view->assign('Enquiry', $enquiry);
		$this->view->setThemeFile($template);
		$this->view->display();
		exit();
	}
	
	protected function _optComplete($isSuccess = true, $goToUrl = '/', $infoTxt = null, $waitSec = null, $template = 'common/optComplete.php')
	{
		if($waitSec == null){ if($isSuccess){ $waitSec = 3; }else{ $waitSec = 5; } }
		if($infoTxt == null){ if($isSuccess){ $infoTxt = '操作成功！'; }else{ $infoTxt = '操作失败'; } }
		
		$this->view->assign('IsSuccess', $isSuccess);
		$this->view->assign('GoToUrl', $goToUrl);
		$this->view->assign('InfoTxt', $infoTxt);
		$this->view->assign('WaitSec', $waitSec);
		
		$this->view->setAutoDisplay(false);
		$this->view->setThemeFile($template);
		$this->view->display();
		exit();
	}
}